﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Text;
using WebCrawler.ProcessingFiles.CategoryClasses;

namespace WebCrawler.ProcessingFiles
{
    public partial class ProcessBrands : System.Web.UI.Page
    {
        NewEggClasses vendor = new NewEggClasses();
        int vendorID = 1;

        Exception ex = new Exception();
        public String errorPage = "ProcessBrands";

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {

                try
                {
                    ProductDataDataContextDataContext dc = new ProductDataDataContextDataContext();

                    var query = (from b in dc.Brands where b.process == 0 select b).FirstOrDefault();

                    if (query != null)
                    {


                        string strResult = "";
                        string currURL = query.brandURL;


                        WebResponse objResponse;
                        WebRequest objRequest = System.Net.HttpWebRequest.Create(currURL);

                        objResponse = objRequest.GetResponse();

                        using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
                        {
                            strResult = sr.ReadToEnd();
                            sr.Close();
                        }

                        strResult = strResult.Replace("\"", "");
                        strResult = strResult.Replace("#", "");
                        strResult = strResult.Replace("&", "|");

                        strResult = strResult.Remove(0, 10000);


                        NewPage.Text = strResult;

                    }
                    else
                    {
                        GetNextPage(null, null);
                    }
                }
                catch (Exception ex)
                {


                    ProcessErrorText(ex);

                }
            }


            if (Request.QueryString.Count > 0)
            {
                ProductDataDataContextDataContext dc = new ProductDataDataContextDataContext();

                string linktext = "";
                string linkurl = "";
                string catid = "";

                if (Request.QueryString.AllKeys.Contains("link"))
                {
                    linktext = Request.QueryString["link"].ToString().Trim();
                    linktext = linktext.Replace("Link", "");
                    linktext = linktext.Replace("View Details", "");
                    linktext = linktext.Trim();
                }
                if (Request.QueryString.AllKeys.Contains("url"))
                {
                    linkurl = Request.QueryString["url"].ToString().Trim();
                    linkurl = linkurl.Trim();
                }
                if (Request.QueryString.AllKeys.Contains("catid"))
                {
                    catid = Request.QueryString["catid"].ToString().Trim();
                }
                string URL = linkurl;
                string Title = Server.HtmlEncode(linktext);
                string LinkText = Server.HtmlEncode(linktext);
                string CatID = Server.HtmlEncode(catid);

                if (URL.Length > 0 && LinkText.Length > 0)
                {

                    if (URL.Contains(","))
                    {
                        char[] separator = new char[] { ',' };

                        string[] strSplitArr = URL.Split(separator);

                        if (strSplitArr[0].ToString().Trim() == strSplitArr[1].ToString().Trim())
                        {

                            URL = strSplitArr[0];
                        }


                    }

                    if (LinkText.Contains(","))
                    {
                        char[] separator = new char[] { ',' };

                        string[] strSplitArr = LinkText.Split(separator);

                        if (strSplitArr[0].ToString().Trim() == strSplitArr[1].ToString().Trim())
                        {

                            LinkText = strSplitArr[0].ToString().Trim();
                        }


                    }

                    if (Title.Contains(","))
                    {
                        char[] separator = new char[] { ',' };

                        string[] strSplitArr = Title.Split(separator);

                        if (strSplitArr[0].ToString().Trim() == strSplitArr[1].ToString().Trim())
                        {
                            Title = strSplitArr[0].ToString().Trim();
                        }

                    }

                    if (Title.Contains("undefined"))
                    {
                        Title = "";
                    }

                    if (CatID.Contains(","))
                    {
                        char[] separator = new char[] { ',' };

                        string[] strSplitArr = CatID.Split(separator);

                        if (strSplitArr[0].ToString().Trim() == strSplitArr[1].ToString().Trim())
                        {
                            CatID = strSplitArr[0].ToString().Trim();
                        }

                    }

                    if (vendor.VendorName == "NewEgg")
                    {

                        ProceesNewEgg(URL, Title, LinkText, CatID);

                    }
                }

            }

        }

        protected void ProceesNewEgg(string url, string title, string linktext, string catid)
        {
            ProductDataDataContextDataContext dc = new ProductDataDataContextDataContext();


            string URL = url;
            string Title = title;
            string LinkText = linktext;
            string CatID = catid;



            if (URL.Contains("/Store/"))
            {
                if (URL.Contains("SubCategory="))
                {
                    ProcessLinks(LinkText, URL, 3);

                }
                else if (URL.Contains("Category="))
                {
                    ProcessLinks(LinkText, URL, 2);

                }
                else if (URL.Contains("Brand="))
                {
                    ProcessBrand(LinkText, URL, LinkText);

                }

            }


            if (URL.Contains("Item=") && !LinkText.Contains("nbsp;") && LinkText.Length > 0)
            {
                ProcessLinks(LinkText, URL, CatID);
            }


        }




        protected void ProcessLinks(string linktext, string linkurl, int level)
        {
            ProductDataDataContextDataContext dc = new ProductDataDataContextDataContext();



            string LinkText = linktext.Trim();
            string LinkURL = linkurl.Trim();
            LinkURL = LinkURL.Replace("|", "&");

            int Level = level;

            var query = from ct in dc.Categories where ct.catName == LinkText select ct;

            if (query.Count() == 0)
            {
                Category cat = new Category();
                cat.catName = LinkText;
                cat.catURL = LinkURL;
                cat.catShortDesc = LinkText;
                cat.level = Level;
                cat.process = 0;
                cat.vendorID = vendorID;
                cat.exclude = 0;

                dc.Categories.InsertOnSubmit(cat);
                dc.SubmitChanges();

            }
        }

        protected void ProcessLinks(string linktext, string linkurl, string catid)
        {
            ProductDataDataContextDataContext dc = new ProductDataDataContextDataContext();


            string LinkText = linktext.Trim();
            string LinkURL = linkurl.Trim();
            LinkURL = LinkURL.Replace("|", "&");

            string CatID = catid.Trim();
            int CatId = 0;

           

            if (!string.IsNullOrEmpty(CatID) && Int32.TryParse(CatID, out CatId))
            {
                CatId = int.Parse(CatID);
            }
           



            var query = from pl in dc.ProductLinks where pl.productName == LinkText select pl;

            if (query.Count() == 0)
            {
                ProductLink p = new ProductLink();
                p.productName = LinkText;
                p.productURL = LinkURL;
                p.process = 0;
                p.vendorID = vendorID;
                p.catID = CatId;
                


                dc.ProductLinks.InsertOnSubmit(p);
                dc.SubmitChanges();
            }


        }

        protected void ProcessBrand(string linktext, string linkurl, string linktitle)
        {
            ProductDataDataContextDataContext dc = new ProductDataDataContextDataContext();


            string LinkText = linktext.Trim();
            string LinkURL = linkurl.Trim();
            LinkURL = LinkURL.Replace("|", "&");

            if (LinkText.Length == 0)
            {
                int a = LinkURL.IndexOf("name=");
                int b = LinkURL.Length;
                int c = a + 5;

                LinkText = LinkURL.Substring(c, b - c);
            }


            var query = from b in dc.Brands where b.brandName == LinkText select b;

            if (query.Count() == 0)
            {
                Brand b = new Brand();
                b.brandName = Server.HtmlEncode(LinkText);
                b.brandURL = LinkURL;
                b.process = 0;


                dc.Brands.InsertOnSubmit(b);
                dc.SubmitChanges();
            }

        }



        protected void ProcessErrorText(Exception ex)
        {

            StringBuilder errorText = new StringBuilder();

            if (ex != null)
            {
                if (ex.Source != null)
                {
                    errorText.Append(errorPage + " :source: " + ex.Source);
                }
                if (ex.Message != null)
                {
                    errorText.Append(errorPage + " :message: " + ex.Message);
                }
                if (ex.InnerException != null)
                {
                    errorText.Append(errorPage + " :innerexception: " + ex.InnerException);
                }
                if (ex.TargetSite != null)
                {
                    errorText.Append(errorPage + " :target: " + ex.TargetSite);
                }

            }
            else
            {
                errorText.Append("unknown error");
            }

            ProductDataDataContextDataContext dce = new ProductDataDataContextDataContext();

            WebCrawler wc = new WebCrawler();
            wc.webCrawlerErrorText = errorText.ToString();

            dce.WebCrawlers.InsertOnSubmit(wc);
            dce.SubmitChanges();
        }


        protected void GetNextPage(object sender, EventArgs e)
        {
            Response.Redirect("/ProcessingFiles/RedirectForm.aspx?Type=1&Name=Brand");
        }

    }
}